<?php 
$this->headLink(array('href' => '/cms/styles/style.css', 'rel' => "stylesheet", 'type' => "text/css"));
$this->headLink(array('href' => '/cms/styles/data-table.css', 'rel' => "stylesheet", 'type' => "text/css"));

$this->headScript()
	->appendFile('/cms/scripts/dojo/dojo.js','',array('djConfig'=> 'parseOnLoad:true, isDebug:false'))
	->setAllowArbitraryAttributes(true);
	
$this->headScript(Zend_View_Helper_HeadScript::FILE,'/cms/scripts/shadowbox/build/adapter/shadowbox-base.js');
$this->headScript(Zend_View_Helper_HeadScript::FILE,'/cms/scripts/shadowbox/build/shadowbox.js');

$this->headScript(Zend_View_Helper_HeadScript::FILE,'/cms/scripts/content.js');
//$this->headScript(Zend_View_Helper_HeadScript::FILE,'/cms/scripts/Popup.js');
$this->headScript(Zend_View_Helper_HeadScript::FILE,'/cms/scripts/TableData.js');
$this->headScript(Zend_View_Helper_HeadScript::SCRIPT,"
	var galleryId = {$this->gallery_id};");
$this->headScript(Zend_View_Helper_HeadScript::SCRIPT,<<<EOD

dojo.require("dijit.InlineEditBox");

var shadowbox_callback = function () {};

reset_shadowbox_callback = function () {
	shadowbox_callback = function () {};
} 

Shadowbox.loadSkin('classic', '/cms/scripts/shadowbox/src/skin');
Shadowbox.loadLanguage('ru', '/cms/scripts/shadowbox/build/lang');
Shadowbox.loadPlayer(['html', 'iframe', 'img', 'swf'], '/cms/scripts/shadowbox/build/player');

window.onload = function () {
	Shadowbox.init(
    	{
			modal:		true,
			onClose: function() { shadowbox_callback() }
		}
	);
	
	newsTable = new tableData('mainGalleryTable');
	
	
	newsTable.registerWidget('image_name',
		function (params) {
			var divEl = document.createElement('DIV');
			divEl.innerHTML = params['value']=='' ? 'Нет заголовка' : params['value'];
			
			new dijit.InlineEditBox({
									autoSave:true,
									renderAsHtml:true,
									onChange: function (value) {
											var temp_form = dojo.create('form');
											dojo.create('input',{'name':'name','value':value},temp_form);
											
											dojo.xhrPost({
											    url: "/cms/gallery/change/gallery_id/"+galleryId+"/id/"+params['id'],
											    form: temp_form,
											    load: function(response, ioArgs){
											      return response;
											    },
											    error: function(response, ioArgs){
											      alert(response);
											      return response;
											    },
											    handleAs: "text"
											  });
										}
								}, divEl);
			
			return divEl;
		});
		
		
	newsTable.registerWidget('image_delete',
		function (params) {
			var divEl = document.createElement('DIV');
			divEl.className = 'icon';
			
			var imgEl = document.createElement('IMG');
			imgEl.src = '/cms/images/cross.png';
			
			var aEl = document.createElement('A');
			aEl.href = 'javascript:;';
			aEl.onclick = function () {
					callData(['image_id',params['id'],'act','del']);
				}
			
			
			divEl.appendChild(aEl);
			aEl.appendChild(imgEl);
			
			return divEl;
		});
		
	callData([]);
}


var addGallery = function() {
	callData(['act','add']);
}
	  
var callData = function (params) {
//alert("/cms/gallery/data/id/"+galleryId+'/'+params.join('/'));
	dojo.xhrGet({
	    url: "/cms/gallery/data/id/"+galleryId+'/'+params.join('/'),
	    load: function(responseObject, ioArgs){
		  newsTable.draw(responseObject);
	      return responseObject;
	    },
	    error: function(response, ioArgs){
	      alert("An error occurred, with response: " + response);
	      return response;
	    },
	    handleAs: "json",
	    sync: true
	  });
}


EOD
);


$this->layout()->body_attr = 'class="modal"';
?>
<p class="stnd_font">
  <button class="okButton" onclick="addGallery()"><img src="/cms/images/plus.png" width="16" height="16" align="absmiddle" /> Добавить изображение</button></p>
<table width="100%" cellspacing="0" class="table-data">
<thead>
  <tr>
    <th nowrap="nowrap" scope="col">Заголовок</th>
<?php for ($t=0;$t<count($this->image_types);$t++) {?>
    <th width="100" nowrap="nowrap" scope="col"><?= $this->image_types[$t][1] ?></th>
<?php } ?>
<?php for ($m=0;$m<count($this->data_map);$m++) {?>
    <th width="100" nowrap="nowrap" scope="col"><?= $this->data_map[$m] ?></th>
<?php } ?>
    <th width="34" nowrap="nowrap" scope="col">&nbsp;</th>
  </tr>
</thead>
<tbody id="mainGalleryTable"></tbody>
</table>
